{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9aeb16ba-d6d3-4874-9e3c-842b16bf6dca",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:01.127128Z",
     "iopub.status.busy": "2025-02-14T01:27:01.126811Z",
     "iopub.status.idle": "2025-02-14T01:27:07.018071Z",
     "shell.execute_reply": "2025-02-14T01:27:07.017411Z",
     "shell.execute_reply.started": "2025-02-14T01:27:01.127109Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.cloud.aliyuncs.com/pypi/simple\n",
      "Collecting ultralytics\n",
      "  Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/14/ed/4c5927190cfc2535d6078c44f0119732d4df2bd04d7c86ab6c2817439041/ultralytics-8.3.75-py3-none-any.whl (914 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m914.9/914.9 kB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: numpy<=2.1.1,>=1.23.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (1.26.4)\n",
      "Requirement already satisfied: matplotlib>=3.3.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (3.9.3)\n",
      "Requirement already satisfied: opencv-python>=4.6.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (4.10.0.84)\n",
      "Requirement already satisfied: pillow>=7.1.2 in /usr/local/lib/python3.10/site-packages (from ultralytics) (11.0.0)\n",
      "Requirement already satisfied: pyyaml>=5.3.1 in /usr/local/lib/python3.10/site-packages (from ultralytics) (6.0.2)\n",
      "Requirement already satisfied: requests>=2.23.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (2.32.3)\n",
      "Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.10/site-packages (from ultralytics) (1.12.0)\n",
      "Requirement already satisfied: torch>=1.8.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (2.3.1)\n",
      "Requirement already satisfied: torchvision>=0.9.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (0.18.1)\n",
      "Requirement already satisfied: tqdm>=4.64.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (4.67.0)\n",
      "Requirement already satisfied: psutil in /usr/local/lib/python3.10/site-packages (from ultralytics) (6.1.0)\n",
      "Requirement already satisfied: py-cpuinfo in /usr/local/lib/python3.10/site-packages (from ultralytics) (9.0.0)\n",
      "Requirement already satisfied: pandas>=1.1.4 in /usr/local/lib/python3.10/site-packages (from ultralytics) (2.2.3)\n",
      "Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.10/site-packages (from ultralytics) (0.13.2)\n",
      "Collecting ultralytics-thop>=2.0.0 (from ultralytics)\n",
      "  Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/a6/10/251f036b4c5d77249f9a119cc89dafe8745dc1ad1f1a5f06b6a3988ca454/ultralytics_thop-2.0.14-py3-none-any.whl (26 kB)\n",
      "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (1.3.1)\n",
      "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (0.12.1)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (4.55.0)\n",
      "Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (1.4.7)\n",
      "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (24.2)\n",
      "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (3.2.0)\n",
      "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/site-packages (from pandas>=1.1.4->ultralytics) (2024.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/site-packages (from pandas>=1.1.4->ultralytics) (2024.2)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (3.4.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (3.10)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (2.2.3)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (2024.8.30)\n",
      "Requirement already satisfied: filelock in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.16.1)\n",
      "Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (4.12.2)\n",
      "Requirement already satisfied: sympy in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (1.13.3)\n",
      "Requirement already satisfied: networkx in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.4.2)\n",
      "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.1.4)\n",
      "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (2024.6.1)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.105)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.105)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.105)\n",
      "Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (8.9.2.26)\n",
      "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.3.1)\n",
      "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (11.0.2.54)\n",
      "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (10.3.2.106)\n",
      "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (11.4.5.107)\n",
      "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.0.106)\n",
      "Requirement already satisfied: nvidia-nccl-cu12==2.20.5 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (2.20.5)\n",
      "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (12.1.105)\n",
      "Requirement already satisfied: triton==2.3.1 in /usr/local/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (2.3.1)\n",
      "Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/site-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.8.0->ultralytics) (12.6.77)\n",
      "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->ultralytics) (1.16.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/site-packages (from jinja2->torch>=1.8.0->ultralytics) (2.1.5)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/site-packages (from sympy->torch>=1.8.0->ultralytics) (1.3.0)\n",
      "\u001b[33mDEPRECATION: pytorch-lightning 1.7.7 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mInstalling collected packages: ultralytics-thop, ultralytics\n",
      "Successfully installed ultralytics-8.3.75 ultralytics-thop-2.0.14\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install ultralytics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "49a3babf-132a-4e26-a4ce-ca70c7710643",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:09.171914Z",
     "iopub.status.busy": "2025-02-14T01:27:09.171568Z",
     "iopub.status.idle": "2025-02-14T01:27:21.858518Z",
     "shell.execute_reply": "2025-02-14T01:27:21.857977Z",
     "shell.execute_reply.started": "2025-02-14T01:27:09.171890Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Creating new Ultralytics Settings v0.0.6 file ✅ \n",
      "View Ultralytics Settings with 'yolo settings' or at '/root/.config/Ultralytics/settings.json'\n",
      "Update Settings with 'yolo settings key=value', i.e. 'yolo settings runs_dir=path/to/dir'. For help see https://docs.ultralytics.com/quickstart/#ultralytics-settings.\n"
     ]
    }
   ],
   "source": [
    "from ultralytics import YOLO\n",
    "import matplotlib.pyplot as plt\n",
    "from modelscope.pipelines import pipeline\n",
    "from modelscope.utils.constant import Tasks\n",
    "import cv2\n",
    "import json\n",
    "import os.path\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "99b73bea-bf5b-4381-b2cb-3876d913e817",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:21.859731Z",
     "iopub.status.busy": "2025-02-14T01:27:21.859318Z",
     "iopub.status.idle": "2025-02-14T01:27:21.862344Z",
     "shell.execute_reply": "2025-02-14T01:27:21.861856Z",
     "shell.execute_reply.started": "2025-02-14T01:27:21.859712Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "file_path = \"/mnt/workspace/b.mp4\"\n",
    "frameRate = 25\n",
    "offset = 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "39b93fd1-ec36-4fe8-bb17-b96385a7df42",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:21.863192Z",
     "iopub.status.busy": "2025-02-14T01:27:21.862963Z",
     "iopub.status.idle": "2025-02-14T01:27:21.868809Z",
     "shell.execute_reply": "2025-02-14T01:27:21.868293Z",
     "shell.execute_reply.started": "2025-02-14T01:27:21.863174Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/mnt/workspace/b_25_12.json'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "directory, filename = os.path.split(file_path)\n",
    "# 分离文件名和扩展名\n",
    "filename_without_ext, ext = os.path.splitext(filename)\n",
    "\n",
    "out_path = os.path.join(directory,f\"{filename_without_ext}_{frameRate}_{offset}.json\")\n",
    "out_path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fe2aff50-e583-418c-9b54-111400afeff9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:21.869908Z",
     "iopub.status.busy": "2025-02-14T01:27:21.869684Z",
     "iopub.status.idle": "2025-02-14T01:27:33.087234Z",
     "shell.execute_reply": "2025-02-14T01:27:33.086757Z",
     "shell.execute_reply.started": "2025-02-14T01:27:21.869893Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-14 09:27:22,665 - modelscope - WARNING - Model revision not specified, use revision: v1.0.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Model to directory: /mnt/workspace/.cache/modelscope/damo/cv_resnet50_pedestrian-attribute-recognition_image\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-14 09:27:23,572 - modelscope - WARNING - Model revision not specified, use revision: v1.0.0\n",
      "2025-02-14 09:27:23,889 - modelscope - INFO - initiate model from /mnt/workspace/.cache/modelscope/hub/damo/cv_resnet50_pedestrian-attribute-recognition_image\n",
      "2025-02-14 09:27:23,890 - modelscope - INFO - initiate model from location /mnt/workspace/.cache/modelscope/hub/damo/cv_resnet50_pedestrian-attribute-recognition_image.\n",
      "2025-02-14 09:27:23,894 - modelscope - INFO - initialize model from /mnt/workspace/.cache/modelscope/hub/damo/cv_resnet50_pedestrian-attribute-recognition_image\n",
      "/usr/local/lib/python3.10/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
      "  warnings.warn(\n",
      "/usr/local/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=None`.\n",
      "  warnings.warn(msg)\n",
      "2025-02-14 09:27:24,211 - modelscope - WARNING - No preprocessor field found in cfg.\n",
      "2025-02-14 09:27:24,212 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n",
      "2025-02-14 09:27:24,212 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'model_dir': '/mnt/workspace/.cache/modelscope/hub/damo/cv_resnet50_pedestrian-attribute-recognition_image'}. trying to build by task and model information.\n",
      "2025-02-14 09:27:24,213 - modelscope - WARNING - No preprocessor key ('pedestrian-attribute-recognition', 'pedestrian-attribute-recognition') found in PREPROCESSOR_MAP, skip building preprocessor.\n",
      "2025-02-14 09:27:30,391 - modelscope - WARNING - Model revision not specified, use revision: v1.1.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Model to directory: /mnt/workspace/.cache/modelscope/damo/cv_tinynas_human-detection_damoyolo\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-02-14 09:27:31,293 - modelscope - WARNING - Model revision not specified, use revision: v1.1.0\n",
      "2025-02-14 09:27:31,689 - modelscope - INFO - initiate model from /mnt/workspace/.cache/modelscope/hub/damo/cv_tinynas_human-detection_damoyolo\n",
      "2025-02-14 09:27:31,689 - modelscope - INFO - initiate model from location /mnt/workspace/.cache/modelscope/hub/damo/cv_tinynas_human-detection_damoyolo.\n",
      "2025-02-14 09:27:31,694 - modelscope - INFO - initialize model from /mnt/workspace/.cache/modelscope/hub/damo/cv_tinynas_human-detection_damoyolo\n",
      "/usr/local/lib/python3.10/site-packages/timm/models/layers/__init__.py:48: FutureWarning: Importing from timm.models.layers is deprecated, please import via timm.layers\n",
      "  warnings.warn(f\"Importing from {__name__} is deprecated, please import via timm.layers\", FutureWarning)\n",
      "2025-02-14 09:27:33,080 - modelscope - WARNING - No val key and type key found in preprocessor domain of configuration.json file.\n",
      "2025-02-14 09:27:33,081 - modelscope - WARNING - Cannot find available config to build preprocessor at mode inference, current config: {'train': {'mosaic_mixup': {'mosaic_prob': 1.0, 'mosaic_scale': [0.1, 2.0], 'mosaic_size': [640, 640], 'mixup_prob': 0.15, 'mixup_scale': [0.5, 1.5], 'degrees': 10.0, 'translate': 0.2, 'shear': 2.0}, 'transform': {'image_mean': [0.0, 0.0, 0.0], 'image_std': [1.0, 1.0, 1.0], 'image_max_range': [640, 640], 'flip_prob': 0.5, 'autoaug_dict': {'box_prob': 0.3, 'num_subpolicies': 5, 'scale_splits': [2048, 10240, 51200], 'autoaug_params': [6, 9, 5, 3, 3, 4, 2, 4, 4, 4, 5, 2, 4, 1, 4, 2, 6, 4, 2, 2, 2, 6, 2, 2, 2, 0, 5, 1, 3, 0, 8, 5, 2, 8, 7, 5, 1, 3, 3, 3]}}}, 'evaluation': {'transform': {'image_mean': [0.0, 0.0, 0.0], 'image_std': [1.0, 1.0, 1.0], 'image_max_range': [640, 640], 'flip_prob': 0.0}}, 'model_dir': '/mnt/workspace/.cache/modelscope/hub/damo/cv_tinynas_human-detection_damoyolo'}. trying to build by task and model information.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "model_id = 'damo/cv_resnet50_pedestrian-attribute-recognition_image'\n",
    "pedestrian_attribute_recognition = pipeline(Tasks.pedestrian_attribute_recognition, model=model_id)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "64198123-d7fa-4aef-83bb-397c6c4df32f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:33.088620Z",
     "iopub.status.busy": "2025-02-14T01:27:33.087999Z",
     "iopub.status.idle": "2025-02-14T01:27:33.751316Z",
     "shell.execute_reply": "2025-02-14T01:27:33.750789Z",
     "shell.execute_reply.started": "2025-02-14T01:27:33.088599Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Load a COCO-pretrained YOLO11n model\n",
    "model = YOLO(\"yolo11x.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dd4ba508-9ef1-47cd-bd85-f898f8b47a7c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:33.752269Z",
     "iopub.status.busy": "2025-02-14T01:27:33.751979Z",
     "iopub.status.idle": "2025-02-14T01:27:33.805971Z",
     "shell.execute_reply": "2025-02-14T01:27:33.805409Z",
     "shell.execute_reply.started": "2025-02-14T01:27:33.752246Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "video = cv2.VideoCapture(file_path)\n",
    "video.isOpened()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d65fe3c5-07cc-423e-9fd4-df9edce852c7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:33.806712Z",
     "iopub.status.busy": "2025-02-14T01:27:33.806496Z",
     "iopub.status.idle": "2025-02-14T01:27:33.810703Z",
     "shell.execute_reply": "2025-02-14T01:27:33.810042Z",
     "shell.execute_reply.started": "2025-02-14T01:27:33.806695Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(240.0, 25.0, 9.6)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "totail_frame = video.get(cv2.CAP_PROP_FRAME_COUNT)\n",
    "fps =video.get(cv2.CAP_PROP_FPS)\n",
    "totail_frame,fps,totail_frame/fps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1d862be2-794d-4c74-9517-77160d7f70d1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-14T01:27:33.811608Z",
     "iopub.status.busy": "2025-02-14T01:27:33.811379Z",
     "iopub.status.idle": "2025-02-14T01:27:33.818864Z",
     "shell.execute_reply": "2025-02-14T01:27:33.818349Z",
     "shell.execute_reply.started": "2025-02-14T01:27:33.811587Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1920, 1080)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
    "height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
    "width,height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d130fbc-597c-4d57-91a3-542c2375e8c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def yoloFindPerson(yolo_res):\n",
    "    return \"person\" in list(map(lambda x:x[\"name\"],yolo_res))\n",
    "\n",
    "\n",
    "def coverRawRes(raw_res):\n",
    "    personalDetail = []\n",
    "    for i in range(len(raw_res[\"boxes\"])):\n",
    "        now_box = raw_res[\"boxes\"][i]\n",
    "        now_label = raw_res[\"labels\"][i]\n",
    "        personDetailItem = {\n",
    "            \"box\":now_box,\n",
    "            \"sex\": now_label[0],\n",
    "            \"ageRange\": now_label[1],\n",
    "            \"orient\":now_label[2],\n",
    "            \"hasHat\":now_label[3]==\"Yes\",\n",
    "            \"hasGlass\":now_label[4]==\"Yes\",\n",
    "            \"hasHandBag\":now_label[5]==\"Yes\",\n",
    "            \"hasShoulderBag\":now_label[6]==\"Yes\",\n",
    "            \"hasBackPack\":now_label[7]==\"Yes\",\n",
    "            \"upperWear\":now_label[8],\n",
    "            \"lowerWear\":now_label[9],\n",
    "            \"upperColor\":now_label[10],\n",
    "            \"lowerColor\":now_label[11]\n",
    "        }\n",
    "        personalDetail.append(personDetailItem)\n",
    "    return personalDetail\n",
    "\n",
    "logs=[]\n",
    "frame_count = 0\n",
    "do_count = 0\n",
    "while True:\n",
    "    ret, frame = video.read()\n",
    "    if not ret:\n",
    "        print(\"finish\")\n",
    "        break\n",
    "    if frame_count%frameRate == offset:\n",
    "    # 进行推理\n",
    "        results = model(frame)\n",
    "        yolo_res = json.loads(results[0].to_json())\n",
    "        now_frame_log = {\n",
    "            \"yolo\":yolo_res,\n",
    "            \"timestamp\":frame_count\n",
    "        }\n",
    "        if yoloFindPerson(yolo_res):\n",
    "            print(\"do net50 cnn\")\n",
    "            now_frame_log[\"personDetail\"]=coverRawRes(pedestrian_attribute_recognition(frame))\n",
    "        else:\n",
    "            now_frame_log[\"personDetail\"]=[]\n",
    "        logs.append(now_frame_log)\n",
    "        do_count+=1\n",
    "        #print(f\"do yolo on model frame {frame_count}\")\n",
    "    frame_count += 1\n",
    "\n",
    "video.release()\n",
    "with open(out_path,\"w\") as file:\n",
    "    file.write(json.dumps({\n",
    "        \"fps\":int(fps),\n",
    "        \"logs\":logs,\n",
    "        \"size\":[width,height]\n",
    "    }))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
